/*
 * 作者: 侯晓宇
 * 时间: 2017-01-24
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {
	require("jquery");

	var c = require('app/utils/CommonUtils');
	var EventUtils = require('app/utils/EventUtils');
	var APIUtils = require('app/utils/APIUtils');
	var win = window;
	var temp_pic_path = "fs://pic_save_temp";
	var cover_val = "";
	var action = {};
	var param = api.pageParam;

	action.addCover = function() {
		return new Promise(function(resolve, reject) {
			api.actionSheet({
				cancelTitle: '取消',
				buttons: ['拍照', '从相册选取']
			}, function(ret, err) {
				var index = ret.buttonIndex;
				var isCamera = null;
				if (index == 1) { // 拍照
					isCamera = true;
				} else if (index == 2) { //从相册选取
					isCamera = false;
				}
				if (isCamera != null) {
					resolve(isCamera);
				}

			});
		});
	}

	action.launchCamera = function() {
		
		return new Promise(function(resolve, reject) {

			api.getPicture({
				sourceType: 'camera', // camera,library
				encodingType: 'jpg',
				mediaValue: 'pic',
				destinationType: 'url',
				allowEdit: false,
				quality: 100,
				saveToPhotoAlbum: false
			}, function(ret, err) {
				if(ret){
					resolve(ret.data);
				}
			});

		});
		
	}
	action.launchPic = function() {
		return new Promise(function(resolve, reject) {

			api.getPicture({
				sourceType: 'library', // camera,library
				encodingType: 'jpg',
				mediaValue: 'pic',
				destinationType: 'url',
				allowEdit: false,
				quality: 100,
				saveToPhotoAlbum: false
			}, function(ret, err) {

				resolve(ret.data);
			});

		});

	}


	action.clipImage = function(srcPath) {
		return new Promise(function(resolve, reject) {
			api.openFrame({
				name: 'ImageClipFrm',
				url: 'ImageClipFrm.html',
				'animation': {
					type: "push", //动画类型（详见动画类型常量）
					subType: "from_right", //动画子类型（详见动画子类型常量）
					duration: 300 //动画过渡时间，默认300毫秒
				},
				rect: {
					x: 0,
					y: 0,
					w: APIUtils.winW(),
					h: APIUtils.winH()
				},
				pageParam: {
					path: srcPath
				}
			});

			EventUtils.listen(api.winName,'updateCover',true,function(data){
				resolve(data);
			});

		});
	}

	action.updateCover = function(path){
		$('#photo_background').backgroundBlur(path);
		$('#photo_cover').attr("src",path);
		$('#photo_button').hide();
		cover_val = path;
	}
	
	action.getCoverVal = function(){
		return cover_val;
	}
	
	return action;
});